/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.gzchengguo.cn
* 注意：
* 本软件为www.gzchengguo.cn开发研制，未经购买不得商用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package link.chengguo.orangemall.modules.system.rest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import link.chengguo.orangemall.dozer.service.IGenerator;
import link.chengguo.orangemall.logging.aop.log.Log;
import link.chengguo.orangemall.modules.domain.OrangeStoreMarketingArea;
import link.chengguo.orangemall.modules.service.OrangeStoreMarketingAreaService;
import link.chengguo.orangemall.modules.service.dto.OrangeStoreMarketingAreaDto;
import link.chengguo.orangemall.modules.service.dto.OrangeStoreMarketingAreaQueryCriteria;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author chengguo
* @date 2020-08-19
*/
@AllArgsConstructor
@Api(tags = "营销区域管理")
@RestController
@RequestMapping("/api/orangeStoreMarketingArea")
public class OrangeStoreMarketingAreaController {

    private final OrangeStoreMarketingAreaService orangeStoreMarketingAreaService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','orangeStoreMarketingArea:list')")
    public void download(HttpServletResponse response, OrangeStoreMarketingAreaQueryCriteria criteria) throws IOException {
        orangeStoreMarketingAreaService.download(generator.convert(orangeStoreMarketingAreaService.queryAll(criteria), OrangeStoreMarketingAreaDto.class), response);
    }

    @GetMapping
    @Log("查询营销区域")
    @ApiOperation("查询营销区域")
    @PreAuthorize("@el.check('admin','orangeStoreMarketingArea:list')")
    public ResponseEntity<Object> getOrangeStoreMarketingAreas(OrangeStoreMarketingAreaQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(orangeStoreMarketingAreaService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增营销区域")
    @ApiOperation("新增营销区域")
    @PreAuthorize("@el.check('admin','orangeStoreMarketingArea:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody OrangeStoreMarketingArea resources){
        return new ResponseEntity<>(orangeStoreMarketingAreaService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改营销区域")
    @ApiOperation("修改营销区域")
    @PreAuthorize("@el.check('admin','orangeStoreMarketingArea:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody OrangeStoreMarketingArea resources){
        orangeStoreMarketingAreaService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除营销区域")
    @ApiOperation("删除营销区域")
    @PreAuthorize("@el.check('admin','orangeStoreMarketingArea:del')")
    @DeleteMapping("{id}")
    public ResponseEntity<Object> delete(@PathVariable("id") Integer id) {
        orangeStoreMarketingAreaService.removeById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
